<?php


namespace app\cms\model\addons;

use app\cms\community\lib\QueryModel;
use iflow\fileSystem\lib\FileList;
use iflow\Response;

class addonsModel extends QueryModel
{
    protected $table = "addons_table";

    /**
     * 获取指定插件信息
     * @param string $addons_ns_code
     * @return array
     */
    public function getAddonsByCode(string $addons_ns_code): array {
        return $this->findInfo([ 'addons_ns_code' => $addons_ns_code ]);
    }

    /**
     * 获取插件列表
     * @param string $addons_name
     * @return Response
     */
    public function getAddonsList(string $addons_name): Response
    {
        $query = $this->whereLike('addons_name', "%${addons_name}%");
        return $this->paginateQuery($query);
    }

    /**
     * 删除插件
     * @param string $addons_ns_code
     * @param bool $removeInstallFile 是否删除已安装的文件
     * @return Response
     */
    public function deleteAddons(string $addons_ns_code, bool $removeInstallFile = true): Response
    {
        $addons = $this->findInfo([
            'addons_ns_code' => $addons_ns_code
        ]);
        if (!$addons) return message() -> nodata('需要删除的插件不存在');

        $delete = $this->deleteData([
            'addons_ns_code' => $addons_ns_code
        ]);

        if ($delete -> data['code'] === 200 && $removeInstallFile) {
            (new FileList()) -> removeDir($addons['addons_install']);
        }

        return $delete;
    }
}
